package com.xjgc.eip.twice.zxjeip2cscbom.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xjgc.common.constant.Consants;
import com.xjgc.common.entity.ReturnResult;
import com.xjgc.common.enums.ReturnStatus;
import com.xjgc.eip.twice.zxjeip2cscbom.entity.ZxjEip2cScbom;
import com.xjgc.eip.twice.zxjeip2cscbom.service.ZxjEip2cScbomService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  生产bom接口
 * </p>
 *
 * @author zhanghongbin
 * @since 2019-11-23
 */
@Api(tags = "生产bom接口")
@RestController
@RequestMapping("/apis/zxjEip2cScbom")
public class ZxjEip2cScbomController {
@Autowired
    ZxjEip2cScbomService zxjEip2cScbomService;
    /**
     * 生产bom接口
     */
    @ApiOperation("生产bom接口")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "amount", value = "查询条数",
                    required = true,dataType = "int")
    })
    @GetMapping(value = "/getData")
    @ResponseBody
    public ReturnResult getData(Integer amount,@RequestHeader(name = "supplierCode",required = false)String supplierCode) {
        ReturnResult returnResult = new ReturnResult();
        QueryWrapper<ZxjEip2cScbom> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("\"IOFLAG\"", Consants.Unread)
                .last("limit " + (amount == null ? 100 : amount));
        if(StringUtils.isNotBlank(supplierCode)){
            queryWrapper.eq("\"SUPPLIER_CODE\"",supplierCode);
        }
        try {
            List<ZxjEip2cScbom> result=zxjEip2cScbomService.list(queryWrapper);

            //更新查询的数据为已读
            if(result!=null&&result.size()>0){
                for (ZxjEip2cScbom zed:result) {
                    zed.setIoflag(Consants.read);
                    zed.setUpdateTime(new Date());
                }
                if (zxjEip2cScbomService.updateBatchById(result)){
                    returnResult.setStatus(Integer.valueOf(ReturnStatus.OK.getCode()));
                    returnResult.setData(result);
                }else {
                    returnResult.setStatus(Integer.valueOf(ReturnStatus.FAIL.getCode()));
                    returnResult.setMessage("请求异常");
                }
            }
        }catch (Exception e){
            returnResult.setStatus(Integer.valueOf(ReturnStatus.FAIL.getCode()));
            returnResult.setMessage("请求异常");
        }
        return returnResult;
    }
    @GetMapping(value="/list")
    public Result<IPage<ZxjEip2cScbom>> queryPgList(ZxjEip2cScbom Scbom,
                                                  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                                  HttpServletRequest req) {
        Result<IPage<ZxjEip2cScbom>> result = zxjEip2cScbomService.queryPageList(Scbom,pageNo,pageSize,req);
        return result;
    }
}

